<template>
	<view>
		<view class="table">
			<view class="table-header">
				<view class="header-item">序号</view>
				<view class="header-item">项目</view>
				<view class="header-item">金额（元）</view>
			</view>
			<view class="table-body">
				<view v-for="(item, index) in salaryItems" :key="index" class="table-row">
					<view class="cell">{{ formatIndex(index + 1) }}</view>
					<view class="cell">{{ item.project }}</view>
					<view class="cell">{{ formatNumber(item.amount) }}</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import isHaveBug from "../../../utils/bug";
import {
		getSalaryData
	} from "@/api/work/salaryData";
	export default {
		data() {
			return {
				isQGsh: true,
				title: "",
				salaryItems: [],
			};
		},
		onLoad(options) {
			if (isHaveBug("HR-APP-011")) {
				this.isQGsh = false
			}
			const {
				salaryData
			} = options;
			// 解析 salaryData 参数
			const salary = JSON.parse(salaryData);
			this.title = `${salary.periodYear}年${salary.periodMonth}月工资明细`;
			uni.setNavigationBarTitle({
				title: this.title,
			});

			this.salaryItems = [{
					project: "基本工资",
					amount: salary.basic,
				},
				{
					project: "岗位工资",
					amount: salary.post,
				},
				{
					project: "奖励工资",
					amount: salary.position,
				},
				{
					project: "绩效工资",
					amount: salary.house,
				},
				{
					project: "补贴合计",
					amount: salary.performance,
				},
				{
					project: "保险合计",
					amount: salary.grsb,
				},
				{
					project: "公积金合计",
					amount: salary.grgjj,
				},
				{
					project: "加班工资",
					amount: salary.gz996,
				},
				{
					project: "请假扣款",
					amount: salary.jqkk,
				},
				{
					project: "应发工资",
					amount: salary.yfgz,
				},
				{
					project: "实发工资",
					amount: salary.sfgz,
				},
			];
		},
		methods: {
			formatIndex(index) {
				return index.toString().padStart(2, "0");
			},
			formatNumber(number) {
				if (number){
					let str = number.toFixed(2).toString();
					let parts = str.split('.'); 
					let integerPart = parts[0];
					let decimalPart = parts[1]; // 插入千位分隔符 
					if (this.isQGsh){
						return "￥" + integerPart.replace(/(\d)(?=(?:\d{3})+$)/g, '$1,') + "." + decimalPart
					}else{
						return "￥" + integerPart
					}
					
				}else{
					if (this.isQGsh){
						return "￥0.00"
					}else{
						return "￥0"
					}
				}
			}
		},
	};
</script>

<style scoped>
	/* 页面整体样式 */
	view {
		padding: 15px;
		background-color: #f5f5f5;
	}

	.title {
		font-size: 18px;
		font-weight: bold;
	}

	/* 表格样式 */
	.table {
		background-color: #ffffff;
		border-radius: 8px;
		box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
		overflow: hidden;
	}

	.table-header {
		display: flex;
		background-color: #f5f5f5;
		padding: 0px 10px;
		border-bottom: 0.5px solid #e0e0e0;
	}

	.header-item {
		flex: 1;
		font-weight: bold;
		text-align: center;
	}

	.table-body {
		padding: 0px 10px;
	}

	.table-row {
		display: flex;
		border-bottom: 0.5px solid #e0e0e0;
		padding: 10px 0;
	}

	.cell {
		flex: 1;
		text-align: center;
	}
</style>